package cn.dnf.basic.controller;

import cn.dnf.basic.exception.BusinessException;
import cn.dnf.basic.service.IVerifyCodeService;
import cn.dnf.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 图形验证码接口
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key){
        try {
                String baseStr = verifyCodeService.getVerifyCode(key);
                return JsonResult.me().setResultObj(baseStr);
        } catch (BusinessException e) {
            e.printStackTrace();
                return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("获取图形验证码失败,请稍后重试");
        }
    }

    /**
     * 短信验证码 接收参数:dto
     *                   map接收
     *  phone
     *  imageCode:
     *  imageCodeKey:
     *
     */
    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody Map<String,String> map ){
        try {
            verifyCodeService.smsCode(map);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙,稍后重试");
        }

    }

    /**
     * 绑定验证码
     * @param map
     * @return
     */
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map ){
        try {
            verifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("系统繁忙,稍后重试");
        }

    }
}
